package com.song.bingclient;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.collections.CollectionUtils;

import com.song.corebean.PackageMessage;
//SimpleChannelInboundHandler<PackageMessage>
public class GridClientHandler extends ChannelInboundHandlerAdapter {

	public BlockingQueue<PackageMessage> queue = new LinkedBlockingQueue<PackageMessage>();
	
	
	public PackageMessage readMsg() {
		return this.queue.poll();
	}
	
	public boolean isEmpQueue()
	{
		return CollectionUtils.isEmpty(queue);
	}
	
	public void channelRead(ChannelHandlerContext ctx, Object obj)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("客户端接受数据 "+obj);
		if(obj!=null)
		{
			PackageMessage msg = (PackageMessage)obj;
			this.queue.add(msg);
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		// TODO Auto-generated method stub
		cause.printStackTrace();
		ctx.close();
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		ctx.flush();
		ctx.close();
		System.out.println("客户端接受数据结束");
	}

	
	public PackageMessage msg;

}
